package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author BrookPeng
 * @Date 2020/11/26 23:55
 * @Version 1.0
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model) { // 9个入参

        SearchResponseVo searchResponseVo = listFeignClient.search(searchParam);
        // 1:入参条件对象 回显
        model.addAttribute(searchParam);
        // 2:品牌集合 回显
        model.addAttribute("trademarkList", searchResponseVo.getTrademarkList());
        // 3:平台属性集合
        List<SearchResponseAttrVo> attrsList = searchResponseVo.getAttrsList();
        model.addAttribute("attrsList", attrsList);
        // 4:商品结果集
        List<Goods> goodsList = searchResponseVo.getGoodsList();
        model.addAttribute("goodsList", goodsList);
        // 5:分页
        Integer pageNo = searchResponseVo.getPageNo();
        Long totalPages = searchResponseVo.getTotalPages();
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("totalPages", totalPages);
        // 6:回显 品牌 选中项 （品牌：金龙鱼）
        String s = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam", s);
        // 7:平台属性及值选中项 回显 propsParamList
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList", propsParamList);
        // 8:URL + 入参集合
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        // 9:排序
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap", orderMap);

        return "list/index.html";
    }

    // 排序 回显
    private Map buildOrderMap(SearchParam searchParam) {

        Map<String, String> buildOrderMap = new HashMap<>();
        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)) {
            String[] split = StringUtils.split(order, ":");
            buildOrderMap.put("type", split[0]);
            buildOrderMap.put("sort", split[1]);
        } else {
            buildOrderMap.put("type", "1");
            buildOrderMap.put("sort", "desc");
        }
        return buildOrderMap;
    }

    // URL + 入参集合 字符串拼接
    private String buildUrlParam(SearchParam searchParam) {

        // StringBuffer StringBuilder
        StringBuffer stringBuffer = new StringBuffer();
        // http://list.gmall.com/list.html
        // ?keyword=手机&trademark=1:华为
        // &props=1:4G:版本&props=2:骁龙888:CPU型号

        // 关键词
        String keyword = searchParam.getKeyword();
        if (!StringUtils.isEmpty(keyword)) {

            stringBuffer.append("keyword=").append(keyword);
            stringBuffer.append("&");
        }
        // 品牌
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            stringBuffer.append("trademark=").append(trademark);
            stringBuffer.append("&");
        }
        // 平台
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            for (String prop : props) {
                stringBuffer.append("props").append(prop);// &props=2:骁龙888:CPU型号
                stringBuffer.append("&");
            }
        }
        // 排序
        // http://list.gmall.com/list.html
        // ?keyword=手机&trademark=1:华为
        // &props=1:4G:版本&props=2:骁龙888:CPU型号&order=2:desc
        /*String order = searchParam.getOrder();// 1:asc 1:desc 2:asc 2:desc
        if (!StringUtils.isEmpty(order)) {
            stringBuffer.append("order").append(order);
        } else {
            stringBuffer.append("order").append("1:desc");
        }*/

        return "/list.html?" + stringBuffer.toString();
    }

    // 平台属性及值选中项 回显
    private List<Map> buildPropsParamList(SearchParam searchParam) {

        List<Map> propsParamList = null;
        // 平台属性参数
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            propsParamList = new ArrayList<>();// 懒汉式
            for (String prop : props) {// 平台ID:平台属性值:平台属性名称
                String[] split = prop.split(":");
                Map map = new HashMap();
                map.put("attrId", split[0]);
                map.put("attrValue", split[1]);
                map.put("attrName", split[2]);
                propsParamList.add(map);
                return propsParamList;
            }
        }
        return null;
    }

    // 构建回显 品牌 选中项 （品牌：金龙鱼）
    private String buildTrademarkParam(SearchParam searchParam) {

        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = trademark.split(":");
            return "品牌：" + split[1];
        }
        return null;
    }
}
